programming4us
           
 
 
Programming

jQuery 1.3 : Improving a basic form (part 5) - Conditionally displayed fields

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/6/2010 5:48:33 PM

Conditionally displayed fields

Let's further improve the group of fields that ask visitors how they would like to be contacted. Since the text inputs need to be entered only if their corresponding checkboxes are checked, we can hide them, along with their corresponding flags, when the document is initially loaded:

$(document).ready(function() {
$('input.conditional').next('span').andSelf().hide();
});

The fieldset now has its streamlined interface:

To make the text inputs and flags appear, we attach the .click() method to each checkbox. We'll do so within the context of each conditional text input so that we can set a couple of variables for reuse:

$(document).ready(function() {
$('input.conditional').next('span').andSelf().hide()
.end().end()
.each(function() {
var $thisInput = $(this);
var $thisFlag = $thisInput.next('span');
$thisInput.prev('label').find(':checkbox')
.click(function() {
// code continues . . .
});
});

});

Here again we make use of two .end() methods, this time so we can attach the .each() method to the original $('input.conditional') selector.

Now we have a variable for the current text input and the current flag. When the user clicks the checkbox, we see if it is checked; if it is, we show the text input, show the flag, and add the req-label class to the parent<label> element:

$(document).ready(function() {
$('input.conditional').next('span').andSelf().hide()
.end().end()
.each(function() {
var $thisInput = $(this);
var $thisFlag = $thisInput.next('span');
$thisInput.prev('label').find(':checkbox')
.click(function() {
if (this.checked) {
$thisInput.show();
$thisFlag.show();
$(this).parent('label').addClass('req-label');
}

});
});
});

For testing whether a box is checked here, this.checked is preferred because we have direct access to the DOM node via the this keyword. When the DOM node is not so accessible, we can use $('selector').is(':checked') instead, since .is() returns a Boolean (true or false).

We're left with two more things to do:

  1. 1. Ensure that the checkboxes are unchecked when the page initially loads, since some browsers will retain the state of form elements on page refresh.

  2. 2. Add an else condition that hides the conditional elements and removes the req-label class when the checkbox is not checked.

$(document).ready(function() {
$('input.conditional').next('span').andSelf().hide()
.end().end()
.each(function() {
var $thisInput = $(this);
var $thisFlag = $thisInput.next('span');
$thisInput.prev('label').find(':checkbox')
.attr('checked', false)

.click(function() {
if (this.checked) {
$thisInput.show();
$thisFlag.show();
$(this).parent('label').addClass('req-label');
} else {
$thisInput.hide();
$thisFlag.hide();
$(this).parent('label')
.removeClass('req-label');
}

});
});

And that concludes the styling portion of this form makeover. Next, we'll add some client-side validation.

Other -----------------
- Changes to Privacy Risk Management and Compliance in Relation to Cloud Computing
- Cloud Security and Privacy : What Are the Key Privacy Concerns in the Cloud?
- Cloud Security and Privacy : What Is the Data Life Cycle?
- Making Your Site Accessible to Search Engines
- Security Management in the Cloud - Security Vulnerability, Patch, and Configuration Management (part 2)
- Security Management in the Cloud - Security Vulnerability, Patch, and Configuration Management (part 1)
- Security Management in the Cloud - Access Control
- Security Management in the Cloud - IaaS Availability Management
- Security Management in the Cloud - PaaS Availability Management
- Security Management in the Cloud - SaaS Availability Management
- Security Management in the Cloud - Availability Management
- Security Management in the Cloud
- The Art of SEO : Trending, Seasonality, and Seasonal Fluctuations in Keyword Demand
- The Art of SEO : Leveraging the Long Tail of Keyword Demand
- The Art of SEO : Determining Keyword Value/Potential ROI
- Identity and Access Management : Cloud Service Provider IAM Practice
- Identity and Access Management : Cloud Authorization Management
- Identity and Access Management : IAM Practices in the Cloud (part 2) - Federated Identity
- Identity and Access Management : IAM Practices in the Cloud (part 1) - Cloud Identity Administration
- iPad SDK : Keyboard Extensions and Replacements (part 4) - Creating the Calculator
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us